//
//  LeaderLeftMenuViewController.m
//  KIASupervisory
//
//  Created by tan_xin on 14-7-6.
//  Copyright (c) 2014年 MyCompany. All rights reserved.
//

#import "LeaderLeftMenuViewController.h"

#import "ErandApproveListViewController.h"
#import "ErandChangeApproveListViewController.h"
#import "ErandDudaoListByDayViewController.h"
#import "LDDErandApplyListViewController.h"
#import "DudaoErandSearchViewController.h"

#import "EditPasswordViewController.h"
#import "FeedbackViewController.h"
#import "AboutUsViewController.h"

#import "ReportParamInfo.h"

@interface LeaderLeftMenuViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImgV;
@property (weak, nonatomic) IBOutlet UILabel *nameLbl;
@property (weak, nonatomic) IBOutlet UITableView *menuTable;
@property (weak, nonatomic) IBOutlet UIButton *logoutBtn;

@property (nonatomic, strong) NSArray *iconList;
@property (nonatomic, strong) NSArray *titleList;

- (IBAction)logoutAction:(id)sender;

@end

@implementation LeaderLeftMenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (iPhone5) {
        self.menuTable.scrollEnabled = NO;
    } else {
        self.menuTable.scrollEnabled = YES;
    }
    
    self.iconList = [[NSArray alloc] initWithObjects:
                     @"icon_出差审批.png",@"icon_出差变更审批.png",@"icon_出差报告查看.png",
                     @"icon_当日出差督导.png",@"icon_当日出差督导.png",@"icon_修改密码.png",
                     @"icon_关于我们.png", nil];
    self.titleList = [[NSArray alloc] initWithObjects:@"出差审批",@"出差变更审批",@"出差报告查看",
                      @"当日出差督导",@"督导出差行程查询",@"修改密码",@"关于我们", nil];
    
    UserInfo *user = [UserInfo instance];
    NSString *name = [user.info objectForKeyOrNil:@"xm"];
    self.nameLbl.text = name;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    ReportParamInfo *info = [ReportParamInfo instance];
//    if (([[info deptList] count] == 0) &&
//        ([[info dudaoNameList] count] == 0) &&
//        ([[info dudaoGhList] count] == 0)) {
//        
//        [self getErandReportParamList];
//    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//获取督导出差列表查询参数
- (void)getErandReportParamList {
    UserInfo *user = [UserInfo instance];
    NSString *yhm = [user.info objectForKeyOrNil:@"yhm"];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           yhm,@"yhm", nil];
    [self.networkManager requestWithMethod:showUserParamByCondition parameter:param delegate:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - NetworkManagerDelegate
- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFinishedWithResult:(id)data {
    [self.HUD hide:YES];
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        int code = [[data objectForKeyOrNil:@"code"] integerValue];
        if (code == 0) {
            
            if ([data count] > 0) {
                id dataObject = [data objectForKeyOrNil:@"data"];
                if ([dataObject isKindOfClass:[NSArray class]]) {
                    if ([dataObject count] > 0) {
                        NSDictionary *dict = [dataObject objectAtIndex:0];
                        NSString *deptString = [dict objectForKeyOrNil:@"zw"];
                        NSString *ghString = [dict objectForKeyOrNil:@"gh"];
                        NSString *nameString = [dict objectForKeyOrNil:@"xm"];
                        
                        NSArray *deptArr = [deptString componentsSeparatedByString:@","];
                        NSArray *ghArr = [ghString componentsSeparatedByString:@","];
                        NSArray *nameArr = [nameString componentsSeparatedByString:@","];
                        
                        ReportParamInfo *info = [ReportParamInfo instance];
                        [info setDeptList:deptArr];
                        [info setGonghaoList:ghArr];
                        [info setNameList:nameArr];
                    }
                }
            }
            
        } else {

        }
    }
}

- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFailedWithError:(NSError *)error {
    
}

#pragma mark - IBAction methods
- (IBAction)logoutAction:(id)sender {
    AppDelegate *dele = [AppDelegate sharedDelegate];
    [dele.ddLeftMenu dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"LeaderMenuCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
    }
    
    UIImageView *iconImgV = (UIImageView *)[cell.contentView viewWithTag:100];
    UILabel *titleLbl = (UILabel *)[cell.contentView viewWithTag:101];
    
    NSInteger row = indexPath.row;
    if ([self.iconList count] > row) {
        NSString *iconImgName = [self.iconList objectAtIndex:row];
        NSString *titleName = [self.titleList objectAtIndex:row];
        
        iconImgV.image = [UIImage imageNamed:iconImgName];
        titleLbl.text = titleName;
    }
    
    return cell;
    
}

#pragma mark - UITableViewDelegate methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 44.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSInteger row = indexPath.row;
    AppDelegate *dele = [AppDelegate sharedDelegate];
    DDMenuController *leftMenu = dele.ddLeftMenu;
    
    UIStoryboard *storyboard = nil;
    SuperViewController *controller = nil;
    switch (row) {
        case 0: { //出差审批
            storyboard = [UIStoryboard storyboardWithName:@"Leader" bundle:nil];
            controller = (ErandApproveListViewController *)[storyboard instantiateViewControllerWithIdentifier:@"LeaderApproveList"];
            break;
        }
        case 1: { //出差变更申请
            storyboard = [UIStoryboard storyboardWithName:@"Leader" bundle:nil];
            controller = (ErandChangeApproveListViewController *)[storyboard instantiateViewControllerWithIdentifier:@"LeaderChangeApproveList"];
            break;
        }
        case 2: { //出差报告查看
            storyboard = [UIStoryboard storyboardWithName:@"Leader" bundle:nil];
            controller = (LDDErandApplyListViewController *)[storyboard instantiateViewControllerWithIdentifier:@"LDDErandApplyList"];
            break;
        }
        case 3: { //当日出差督导
            storyboard = [UIStoryboard storyboardWithName:@"Leader" bundle:nil];
            controller = (ErandDudaoListByDayViewController *)[storyboard instantiateViewControllerWithIdentifier:@"ErandDudaoListByDay"];
            break;
        }
        case 4: { //督导出差行程查询
            storyboard = [UIStoryboard storyboardWithName:@"Leader" bundle:nil];
            controller = (DudaoErandSearchViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DudaoErandSearch"];
            break;
        }
        case 5: { //修改密码
            storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            controller = (EditPasswordViewController *)[storyboard instantiateViewControllerWithIdentifier:@"EditPassword"];
            break;
        }
//        case 6: { //问题反馈
//            storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//            controller = (FeedbackViewController *)[storyboard instantiateViewControllerWithIdentifier:@"FeedBack"];
//            break;
//        }
        case 6: { //关于我们
            storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            controller = (AboutUsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"AboutUs"];
        }
            
        default:
            break;
    }
    
    if (controller != nil) {
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
        [leftMenu setRootController:navController animated:YES];
    }
}

@end
